﻿using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting.Compact;
using System;
using System.Collections.Generic;
using System.Threading;

namespace serilog_test
{
    /// <summary>
    /// https://github.com/serilog/serilog/wiki/Getting-Started
    /// </summary>
    class Program
    {
        /***
         
            详细 -跟踪信息和调试细枝末节;通常仅在异常情况下打开
            Debug- 内部控制流和诊断状态转储，便于查明已识别的问题
            Information- 感兴趣的事件或与外部观察员相关的事件;默认启用的最低日志记录级别
            Warning- 可能出现问题或服务/功能退化的指示器
            Error- 指示应用程序或连接的系统内出现故障
            Fatal- 导致应用程序完全故障的关键错误

         */
        static void Main(string[] args)
        {
            var tmp = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";

            var log = new LoggerConfiguration()
                .Enrich.With(new ThreadIdEnricher())
                .Enrich.WithProperty("appName", "Serilog_App")
                .Enrich.WithProcessId()
                .Enrich.WithProcessName()
                .Enrich.WithMachineName()
                .Enrich.WithEnvironmentUserName()
                .WriteTo.Console(
                    outputTemplate: "{appName} {Timestamp:HH:mm} [{Level}] [{ProcessId}] [{ProcessName}] [{MachineName}] <{EnvironmentUserName}> <{ThreadId}> {Message}{NewLine}{Exception}"
                 )
                .WriteTo.Console(
                    //formatter: new RenderedCompactJsonFormatter()
                    //formatter: new CompactJsonFormatter()
                 )
                .WriteTo.File("logs\\myapp.txt", rollingInterval: RollingInterval.Day)
                .WriteTo.Debug()
                .MinimumLevel.Verbose() 
                .CreateLogger();

            Log.Logger = log;

            Log.Information("The global logger has been configured");

            var s = "马儿";
            Log.Debug("又要{@s}跑，又要{@s}不吃早?", s);


            Log.Verbose("test Verbose");

            int a = 10, b = 0;
            try
            {
                Log.Debug("Dividing {@A} by {@B}", a, b);
                Console.WriteLine(a / b);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Something went wrong");
            }

            var position = new { Latitude = 25, Longitude = 134 };
            var elapsedMs = 34;

            log.Information("Processed {@Position} in {Elapsed:000} ms.", position, elapsedMs);

            var count = 456;
            Log.Information("Retrieved {Count} records", count);

            var fruit = new[] { "Apple", "Pear", "Orange" };
            Log.Information("In my bowl I have {Fruit}", fruit);


            var fruit2 = new Dictionary<string, int> { { "Apple", 1 }, { "Pear", 5 } };
            Log.Information("In my bowl I have {fruit2}", fruit2);



            Log.Information("The time is " + DateTime.Now);

            Log.Information("The time is {Now}", DateTime.Now);


            Log.CloseAndFlush();


            Console.ReadKey();

        }

        class ThreadIdEnricher : ILogEventEnricher
        {
            public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
            {
                logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
                        "ThreadId", Thread.CurrentThread.ManagedThreadId));
            }
        }
    }
}
